# -*- coding: utf-8 -*-
'''
Created on 18-11-2012

@author: LONG HOANG GIANG
'''

import sys
import os
from CrawlerLib import commonlib, html2text
from Model.storymodelv2 import StoryModel
import re
from urlparse import urljoin
import mechanize
import urllib



class DocHanh():
    
    storyURL = ''
    startReading = ''
    baseURL = 'http://dochanh.net/'
    chapterPrefix = "CHAP {0}"
    cookiestr = ''
    
    def __init__(self, url):
        self.startReading = self.detectTypeURL(url)
    
    def detectTypeURL(self, url):
        if re.search(r"/truyen-\d+/", url):
            res = commonlib.loadweb(url, cookiejar=self.cookiestr)
            html = res.get_html()
            startReading = commonlib.extractText(r",start_reading='(.+)',share_title", html, 1)
            if startReading == '': raise Exception, 'Error occurred when detect start chapter URL' 
            return urljoin(self.baseURL, startReading)
        return url
    
    def post(self, chapterId, cookie):
        url = 'http://dochanh.net/singleee/{0}'.format(chapterId)
        print '> get content from {0}'.format(url)
        res = commonlib.loadweb(url, cookie=self.cookiestr)
        html = res.get_html()
        self.cookiestr = res.get_cookie()
        return html
    
    def process(self):
        url = self.startReading
        database = StoryModel("/longhoanggiang/database/{0}".format(DATABASENAME))
        database.open(True)
        while url != '':
            res = commonlib.loadweb(url, cookie=self.cookiestr)
            html = res.get_html()
            self.cookiestr = res.get_cookie()
            chapterId = commonlib.extractText(r"/chap-(\d+)/", url, 1)
            currentChap = commonlib.extractText(r"total_chap=\d+,curent_chap=(\d+),next_chap=\d+", html, 1)
            if currentChap == '': raise Exception, 'Error occurred while get current chapter number'
            currentChap = self.chapterPrefix.format(currentChap)
            content = self.post(chapterId, self.cookiestr)
            content = html2text.html2text(content).strip()
            content = re.sub(ur"\n", "<br />", content)
            url = commonlib.extractText(r"str_next='([^']+)',", html, 1)
            content = "<b>{0}</b><br /><hr /><br />{1}".format(currentChap, content)
#            database.add_story(currentChap, content, 0)
            print "TITLE: {0}".format(currentChap)
            print "CONTENT: {0}".format(content)
        database.close()

if __name__ == '__main__':
    
    DATABASENAME = "phehonnghichthien"
    dochanh = DocHanh("http://dochanh.net/truyen-12492/phe-hon-nghich-thien.html")
    dochanh.process()
